//
//  UIViewController+Album.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/19.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "UIViewController+Album.h"
#import <TZImagePickerController/TZImagePickerController.h>
#import <objc/runtime.h>
#import "LGColor.h"

typedef void(^__ImageBlock)(UIImage *image);
typedef void(^__ImagesBlock)(NSArray <UIImage *>*images);

@interface UIViewController()<TZImagePickerControllerDelegate>
@property (nonatomic,copy) __ImageBlock handle;
@property (nonatomic,copy) __ImagesBlock many;
@end

@implementation UIViewController (Album)

- (void)setMany:(__ImagesBlock)many
{
    objc_setAssociatedObject(self, @selector(many), many, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (__ImagesBlock)many
{
    return objc_getAssociatedObject(self, _cmd);
}

- (__ImageBlock)handle
{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setHandle:(__ImageBlock)handle
{
    objc_setAssociatedObject(self, @selector(handle), handle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)pickerImageAction:(void(^)(UIImage *image))imageBlock
{
    self.handle = imageBlock;
    
    [self getImagePickerWithMaxImagesCount:1];
    
}

- (void)gotoSelectedAssets:(NSMutableArray *)selectedAssets index:(NSInteger)index
{
    TZImagePickerController *picker = [[TZImagePickerController alloc] initWithSelectedAssets:selectedAssets selectedPhotos:nil index:index];
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}

- (void)pickerManyImageCount:(NSInteger)count action:(void(^)(NSArray <UIImage *>*images))imageBlock
{
    
    self.many = imageBlock;
    
    [self getImagePickerWithMaxImagesCount:count];
}

- (void)pickerManyImageCount:(NSInteger)count handle:(void(^)(NSArray <PHAsset *>*images))imageBlock
{
    TZImagePickerController *picker = [self getImagePickerWithMaxImagesCount:count];
    picker.allowCrop = NO;
    picker.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        if (imageBlock)
        {
            imageBlock(assets);
        }
    };
}

- (TZImagePickerController *)getImagePickerWithMaxImagesCount:(NSInteger)count
{
    TZImagePickerController *picker = [[TZImagePickerController alloc] initWithMaxImagesCount:count delegate:self];
    //照片排序
    picker.sortAscendingByModificationDate = YES;
    
    //是否可以选原图
    picker.allowPickingOriginalPhoto = NO;
    
    //不允许选视频
    picker.allowPickingVideo = NO;
    
    //不能选照片和gif
    //        self.allowPickingMultipleVideo = NO;
    
    picker.showSelectBtn = NO;
    
    //        self.allowTakePicture = NO;
    
    picker.allowCrop = YES;
    
    //nav的背景色
//    picker.naviBgColor = TOPCAIL_COLOR;
    //不能发送gif
    picker.allowPickingGif = NO;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    return picker;
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto
{
    if (self.handle)
    {
        self.handle([photos firstObject]);
    }
    
    if (self.many)
    {
        self.many(photos);
    }
}

@end
